var productfileinfoTable;
var menuId;
$(function () {
    getTreeNode();

    $('#searchBtn').on('click', function () {
        productfileinfoTable.fnDraw();
    });
    productfileinfoTable = $("#productfileinfoTable").dataTable({
        "deferRender": true,
        "processing": true,
        "serverSide": true,
        "aaSorting": [
            [1, "asc"]
        ],
        //是否启用详细信息视图
        "detailView":true,
        "detailFormatter":"",
        "ajax": {
            url: base_url + "/productfileinfo/pageList",
            type: "post",
            data: function (d) {
                var obj = {};
                var obj = {
                    "menuId": menuId,
                    "region": $('#region').val(),
                    "issue": $('#issue').val(),
                    "start": d.start,
                    "length": d.length
                }
                return obj;
            }
        },
        "searching": false,
        "ordering": false,
        // "scrollX": true, // X轴滚动条，取消自适应
        /**"fnDrawCallback": function () {
            this.api().column(0).nodes().each(function (cell, i) {
                cell.innerHTML = i + 1;
            });
        },**/
        "columns": [
        {
            "data": null
        }, {
            "data": 'producttype',
            "visible": true
        }, {
            "data": 'issue',
            "visible": true
        }

            , {
                "data": 'cycle',
                "visible": true
            }
            , {
                "data": 'region',
                "visible": true
            }
            , {
                "data": '操作',
                "render": function (data, type, row) {
                    tableData['key' + row.id] = row;
                    return function () {
                        var html = '<p id="' + row.id + '" >' + '<button class="btn btn-warning btn-xs update" type="button">查看</button>  ' + ' ' + '<button class="btn btn-danger btn-xs model_operate" _type="model_del" type="button" onclick="del(this);">删除</button>  ' + '</p>';
                        return html;
                    };
                }
            }

        ],
        "language": {
            "sProcessing": "处理中...",
            "sLengthMenu": "每页 _MENU_ 条记录",
            "sZeroRecords": "没有匹配结果",
            "sInfo": "第 _PAGE_ 页 ( 总共 _PAGES_ 页，_TOTAL_ 条记录 )",
            "sInfoEmpty": "无记录",
            "sInfoFiltered": "(由 _MAX_ 项结果过滤)",
            "sInfoPostFix": "",
            "sSearch": "搜索:",
            "sUrl": "",
            "sEmptyTable": "表中数据为空",
            "sLoadingRecords": "载入中...",
            "sInfoThousands": ",",
            "oPaginate": {
                "sFirst": "首页",
                "sPrevious": "上页",
                "sNext": "下页",
                "sLast": "末页"
            },
            "oAria": {
                "sSortAscending": ": 以升序排列此列",
                "sSortDescending": ": 以降序排列此列"
            }
        },
        "fnCreatedRow": function(nRow, aData, iDataIndex) {
            $('td:eq(0)', nRow).html("<span class='row-details row-details-close' data_id='"+aData.id+"''></span>&nbsp;" +(iDataIndex+1));
        }
    });
    $('#productfileinfoTable').on('click', ' tbody td .row-details',
        function() {
            var nTr = $(this).parents('tr')[0];
            if (productfileinfoTable.fnIsOpen(nTr)) //判断是否已打开
            {
                /* This row is already open - close it */
                $(this).addClass("row-details-close").removeClass("row-details-open");
                productfileinfoTable.fnClose(nTr);
            } else {
                /* Open this row */
                $(this).addClass("row-details-open").removeClass("row-details-close");
                //  alert($(this).attr("data_id"));
                //oTable.fnOpen( nTr,
                // 调用方法显示详细信息 data_id为自定义属性 存放配置ID
                fnFormatDetails(nTr, $(this).attr("data_id"));
            }
        });
    var tableData = {};


    $("#productfileinfoTable").on('click', '.update', function () {
        var id = $(this).parent('p').attr("id");
        var row = tableData['key' + id];
        if (!row) {
            layer.open({
                title: '系统提示',
                content: ("模型信息加载失败，请刷新页面"),
                icon: '2'
            });
            return;
        }
        // base data
        $("#updateModal .form input[name='producttype']").val(row.producttype);
        $("#updateModal .form input[name='issue']").val(row.issue);
        $("#updateModal .form input[name='cycle']").val(row.cycle);
        $("#updateModal .form input[name='filetype']").val(row.filetype);
        $("#updateModal .form input[name='filepath']").val(row.filepath);


        // show
        $('#updateModal').modal({
            backdrop: false,
            keyboard: false
        }).modal('show');
    });
    $("#updateModal").on('hide.bs.modal', function () {
        $("#updateModal .form")[0].reset();

    });


});

function del(obj) {
    layer.confirm('确认删除?', {icon: 3, title: '系统提示'}, function (index) {
        layer.close(index);
        var id = $(obj).parent('p').attr("id");
        var data = {
            "id": id
        };
        $.ajax({
            type: "post",
            data: data,
            dataType: "json",
            url: base_url + "/productfileinfo/deleteProductFileInfo",
            success: function (data) {
                if (data.code == 200) {

                    layer.open({
                        title: '系统提示',
                        content: "成功",
                        icon: '1',
                        end: function (layero, index) {
                            productfileinfoTable.fnDraw();

                        }
                    });
                } else {
                    layer.open({
                        title: '系统提示',
                        content: ("失败"),
                        icon: '2'
                    });
                }
            }
        });

    });


}

function getparent(nodeId, parentList) {
    var arr = $('#treeview5').treeview("getParent", nodeId);
    var a = [arr.text, arr.id];
    if (arr.nodeId != undefined) {
        parentList.push(a);
        return getparent(arr.nodeId, parentList);

    } else {
        return parentList;
    }

}
function getTreeNode() {
    $.ajax({
        type: "post",
        dataType: "json",
        url: base_url + "/product/getTreeNode",
        success: function (defaultData) {
            $('#treeview5').treeview({
                color: "#428bca",
                //icon: "glyphicon glyphicon-stop",
                //selectedIcon: "glyphicon glyphicon-stop",
                //nodeIcon: 'glyphicon glyphicon glyphicon-stop',

                levels: 1,
                data: defaultData,
                onNodeSelected: function (event, data) {
                    $('#id').val(data.id);
                    menuId = data.id;
                    productfileinfoTable.fnDraw();
                    $('#id').val("");
                }
            });

        }
    });

}

function fnFormatDetails(nTr, pdataId) {
    console.log(pdataId);
    $.get(base_url + "/productfileinfo/findFileInfoByWhere/"+pdataId,
        function(json) {
           var sOut="";
           for(var i = 0; i < json.length; i++){
                sOut += '<p >'+json[i].filename+json[i].filetype+'</p>';
           }
            productfileinfoTable.fnOpen(nTr, sOut, 'details');


        });
    //根据配置Id 异步查询数据

}